/**
 * 
 */
package org.xlab.baseframework.listener;

import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.xlab.baseframework.domain.SystemParameter;
import org.xlab.baseframework.exception.SystemParameterNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.service.api.ISystemParameterService;
import org.xlab.baseframework.service.spi.ISystemInitializationListener;
import org.xlab.baseframework.system.PlatformDeclare;
import org.xlab.baseframework.system.SystemRuntimeEnvironment;

/**
 * @author MaKuBEX
 * 
 */
@Component
public class SystemParameterLoader implements ISystemInitializationListener {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 7641730881963119630L;
	private static final Logger log = Logger
			.getLogger(SystemParameterLoader.class);
	@Autowired(required = true)
	@Qualifier("bpf_systemParameterService")
	private ISystemParameterService systemParameterService;
	@Autowired(required = true)
	@Qualifier("bpf_systemParameterServiceMessageSource")
	private MessageSource messageSource;

	/**
	 * @return the messageSource
	 */
	public MessageSource getMessageSource() {
		return messageSource;
	}

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#getSequence()
	 */
	public int getSequence() {
		return PlatformDeclare.SEQUENCE_OF_SYSTEM_PARAMETER_LOADER;
	}

	/**
	 * @return the systemParameterService
	 */
	public ISystemParameterService getSystemParameterService() {
		return systemParameterService;
	}

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#onSystemStarted()
	 */
	public void onSystemStarted() throws SystemRuntimeException {
		String message = null;
		try {
			message = this.messageSource.getMessage(
					"info.listener.runtime.env.loadingparams", null,
					"Loading all system parameters...", Locale.getDefault());
			log.debug(message);
			List<SystemParameter> allParam = this.systemParameterService
					.findAll();
			for (SystemParameter param : allParam) {
				SystemRuntimeEnvironment.setParameter(param);
			}
		} catch (SystemParameterNotFoundException e) {
			message = this.messageSource.getMessage(
					"info.listener.runtime.env.emptyparams", null,
					"There is not any system parameter be found.",
					Locale.getDefault());
			log.warn(message);
		}
	}

	/**
	 * @param messageSource
	 *            the messageSource to set
	 */
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	/**
	 * @param systemParameterService
	 *            the systemParameterService to set
	 */
	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}

}
